//
//  FQPickRollView.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/24.
//

import UIKit

class FQPickRollView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
    let titleLb = QMUILabel(font: .mSFont(20), textColor: .color51)

    var sureStr = ""
    var closeBlock : ((_ model : FQRegionDataModel)->Void)?
    var popup : KLCPopup!
    
    let pickerView = UIPickerView()
    
    var dataSource : [[String : [String : [String : [String]]]]] = []
    var provinceArray : [FQRegionDataModel] = []
    var cityArray : [FQRegionDataModel] = []
//    var areaArray : [String] = []
    var selectProvinceIndex : Int = 0
    
    var dataArray : [FQRegionDataModel] = [] {
        didSet {
            provinceArray = dataArray
            cityArray = dataArray.first!.children
//            areaArray = cityArray.
            pickerView.reloadAllComponents()
        }
    }
    var model : FQFillInfoModel! {
        didSet {
            titleLb.text = model.title
        }
    }
    
    convenience init() {
        self.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 323))
        clipsToBounds = true
        layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        layer.cornerRadius = 22
        backgroundColor = .white
        
        addSubview(titleLb)
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(24)
            make.height.equalTo(28)
        }
        
        let closeBtn = QMUIButton()
        closeBtn.setTitle("确定", for: .normal)
        closeBtn.titleLabel?.font = .mSFont(18)
        closeBtn.setTitleColor(.colorMain, for: .normal)
        closeBtn.addTarget(self, action: #selector(closeAction), for: .touchUpInside)
        addSubview(closeBtn)
        
        closeBtn.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.centerY.equalTo(titleLb)
        }
        
        pickerView.delegate = self
        pickerView.dataSource = self
        addSubview(pickerView)
        
        pickerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLb.snp.bottom).offset(10)
            make.height.equalTo(234)
        }
        
//        let path : URL = Bundle.main.url(forResource: "city", withExtension: "plist")!
//        let data : Data = try! Data(contentsOf: path)
//        dataSource = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [[String : [String : [String : [String]]]]]
//        provinceArray = dataSource.flatMap({ dict in
//            dict.keys
//        })
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        2
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return provinceArray.count
        default:
            return cityArray.count
//        default:
//            return areaArray.count
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            return provinceArray[row].value
        default:
            return cityArray[row].value
//        default:
//            return areaArray[row]
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var lb = view as? QMUILabel
        if lb == nil {
            lb = QMUILabel(font: .rSFont(16), textColor: .black)
            lb!.textAlignment = .center
        }
        lb!.text = self.pickerView(pickerView, titleForRow: row, forComponent: component)
        return lb!
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0:
            selectProvinceIndex = row
//            cityArray = getCityNamesFromProvinceIndex(row)
//            areaArray = getAreaNamesFromProvinceIndex(row, 0)
            cityArray = provinceArray[row].children
            pickerView.reloadComponent(1)
            pickerView.selectRow(0, inComponent: 1, animated: true)
//            pickerView.reloadComponent(2)
//            pickerView.selectRow(0, inComponent: 2, animated: true)
        case 1:
            break
//            areaArray = getAreaNamesFromProvinceIndex(selectProvinceIndex, row)
//            pickerView.reloadComponent(2)
//            pickerView.selectRow(0, inComponent: 2, animated: true)
        default:
            break
        }
    }

    @objc func closeAction() {
//        sureStr = provinceArray[pickerView.selectedRow(inComponent: 0)].value + "-" + cityArray[pickerView.selectedRow(inComponent: 1)].value
        if closeBlock != nil {
            closeBlock!(cityArray[pickerView.selectedRow(inComponent: 1)])
        }
        close()
    }
    
//    func getAreaNamesFromProvinceIndex(_ provinceIndex : Int, _ cityIndex : Int) -> [String] {
//        let tempDic : [String : [String : [String]]] = dataSource[provinceIndex][provinceArray[provinceIndex]]!
//        var array : [[String : [String]]] = []
//        for dict in tempDic.values {
//            array.append(dict)
//        }
//        let dict = array[cityIndex]
//        return dict[cityArray[cityIndex]]!
//    }
//
//    func getCityNamesFromProvinceIndex(_ provinceIndex : Int) -> [String] {
//        let tempDic : [String : [String : [String]]] = dataSource[provinceIndex][provinceArray[provinceIndex]]!
//        var array : [String] = []
//        for dict in tempDic.values {
//            for dic in dict.keys {
//                array.append(dic)
//            }
//        }
//        return array
//    }
    
    func show() {
        popup = KLCPopup(contentView: self, showType: .slideInFromBottom, dismissType: .slideOutToBottom, maskType: .dimmed, dismissOnBackgroundTouch: true, dismissOnContentTouch: false)
        popup.show(with: KLCPopupLayout(horizontal: .center, vertical: .bottom))
    }
    
    func close() {
        popup.dismiss(true)
    }
}
